import 'package:flutter/material.dart';
import '../../manger/user_manger.dart';
import '../model/verification_model.dart';
import 'package:ninetyfour/l10n/app_localizations.dart';
import '../controller/logout_pagefour.dart';
import '../../manger/qysdk_manger.dart';
class LogoutPageThree extends StatefulWidget {
  final String reasonText;
  
  const LogoutPageThree({super.key, required this.reasonText});

  @override
  State<LogoutPageThree> createState() => _LogoutPageThreeState();
}

class _LogoutPageThreeState extends State<LogoutPageThree> {
  List<VerificationModel> yzArray = [];
  
  @override
  void initState() {
    super.initState();
  }

  @override
  void didChangeDependencies() {
    super.didChangeDependencies();
    _loadData();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        toolbarHeight: 40,
        title: Text(AppLocalizations.of(context)!.identityVerification,style: const TextStyle(fontSize: 17)),
        actions: [
          TextButton.icon(
            onPressed: () {
              QYSDK.showQYSession();
            },
            icon: Image.asset('assets/images/zhuxiao0.png'),
            label: Text(isChineseLocale() ? '客服' : 'Customer',style: const TextStyle(fontSize: 16)),
            style: TextButton.styleFrom(
              foregroundColor: Colors.black,
            ),
          ),
        ],
      ),
      body: SingleChildScrollView(
        padding: const EdgeInsets.symmetric(horizontal: 20),
        child: Column(
          crossAxisAlignment: CrossAxisAlignment.start,
          children: [
            const SizedBox(height: 40),
            Text(
              AppLocalizations.of(context)!.selectVerificationMethod,
              style: TextStyle(fontSize: 18, fontWeight: FontWeight.bold),
            ),
            const SizedBox(height: 10),
            Text(
              AppLocalizations.of(context)!.verifyForAccountSecurity,
              style: TextStyle(fontSize: 14, color: Color(0xFF666666)),
            ),
            const SizedBox(height: 35),
            Text(
              AppLocalizations.of(context)!.chooseVerificationMethod,
              style: TextStyle(fontSize: 13, color: Color(0xFF666666)),
            ),
            const SizedBox(height: 20),
            ..._buildVerificationButtons(),
          ],
        ),
      ),
    );
  }

  List<Widget> _buildVerificationButtons() {
    return yzArray.map((model) {
      return Container(
        margin: const EdgeInsets.only(bottom: 20),
        decoration: BoxDecoration(
          color: Colors.white,
          borderRadius: BorderRadius.circular(35),
          boxShadow: [
            BoxShadow(
              color: Colors.blue.withAlpha(20),
              blurRadius: 4,
              offset: Offset.zero,
            ),
          ],
        ),
        child: ListTile(
          leading: Image.asset(_getImagePath(model)),
          title: Text('${model.code} ${model.value}'),
          subtitle: Text(model.status),
          trailing: const Icon(Icons.arrow_forward_ios, size: 14),
          onTap: () => _onVerificationSelected(model),
        ),
      );
    }).toList();
  }

  String _getImagePath(dynamic model) {
    if (yzArray.length == 1) {
      return model.code.isEmpty ? 'assets/images/zhuxiao2.png' : 'assets/images/zhuxiao1.png';
    } else {
      return 'assets/images/zhuxiao${yzArray.indexOf(model) + 1}.png';
    }
  }

  void _loadData() {
    if (UserInfos.shared.token.isNotEmpty && UserInfos.shared.userModel != null) {
      final model = UserInfos.shared.userModel!;
      
      if (model.mobile?.mobile.isNotEmpty ?? false) {
        yzArray.add(VerificationModel(
          status: AppLocalizations.of(context)!.verifyBySms,
          value: model.mobile!.mobile,
          code: model.mobile!.code,
        ));
      }
      
      if (model.email?.email.isNotEmpty ?? false) {
        yzArray.add(VerificationModel(
          status: AppLocalizations.of(context)!.verifyByEmail,
          value: model.email!.email,
          code: '',
        ));
      }
      
      setState(() {});
    }
  }

  void _onVerificationSelected(dynamic model) {
    Navigator.push(
      context,
      MaterialPageRoute(
        builder: (context) => ZhuxiaoStepFourController(
          zxType: model.code.isEmpty ? "email" : "phone",
          reasonText: widget.reasonText,
          vermodel: model,
        ),
      ),
    );
  }
}